package com.zhao.controller.show.plate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.Util.plate.calendar.ProgrammerCalendar;
import com.zhao.pojo.Calendars;
import com.zhao.service.CalendarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author : Mr.zhao
 * @Date: 2022/05/14/16:06
 * @Description: 程序员老黄历
 */
@Controller
@RequestMapping("/Calendar")
public class programmerCalendarController {
    @Autowired
    CalendarService calendarService;

    @RequestMapping("/CalendarData/{id}")
    public String calendarData(@PathVariable String id, Model model){
        if (id.isEmpty()){
            return "redirect:/show/main";
        }
        Calendars calendars = calendarService.getById(id);
        calendars.setBadList(Arrays.asList(calendars.getBad().split("><")));
        calendars.setBad(calendars.getBad().replace("><","  "));
        calendars.setGoodList(Arrays.asList(calendars.getGood().split("><")));
        calendars.setGood(calendars.getGood().replace("><","  "));
        model.addAttribute("calendar",calendars);
        return "show/plate/programmerCalendarOne";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add() {
        Calendars calendars = ProgrammerCalendar.generateCalendar();
        List<Calendars> title = calendarService.list(new QueryWrapper<Calendars>().eq("title", calendars.getTitle()));
        if (title.isEmpty()){
            calendarService.save(calendars);
        }
        return calendars.toString();
    }
}
